package com.senlingspace.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.common.utils.SecurityUtils;
import com.senlingspace.system.domain.SlkjEquipmentShop;
import com.senlingspace.system.mapper.SlkjEquipmentShopMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjEquipmentMapper;
import com.senlingspace.system.domain.SlkjEquipment;
import com.senlingspace.system.service.ISlkjEquipmentService;

/**
 * 设备Service业务层处理
 * 
 * @author SenlingSpace
 * @date 2024-09-26
 */
@Service
public class SlkjEquipmentServiceImpl implements ISlkjEquipmentService 
{
    private static final Logger log = LoggerFactory.getLogger(SlkjEquipmentServiceImpl.class);
    @Autowired
    private SlkjEquipmentMapper slkjEquipmentMapper;
    @Autowired
    private SlkjEquipmentShopMapper slkjEquipmentShopMapper;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public SlkjEquipment selectSlkjEquipmentById(Long id)
    {
        return slkjEquipmentMapper.selectSlkjEquipmentById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param slkjEquipment 设备
     * @return 设备
     */
    @Override
    public List<SlkjEquipment> selectSlkjEquipmentList(SlkjEquipment slkjEquipment)
    {
        PageUtils.startPage();
        List<SlkjEquipment> slkjEquipments = slkjEquipmentMapper.selectSlkjEquipmentList(slkjEquipment);
        return slkjEquipments;
    }

    /**
     * 新增设备
     * 
     * @param slkjEquipment 设备
     * @return 结果
     */
    @Override
    public int insertSlkjEquipment(SlkjEquipment slkjEquipment)
    {
        slkjEquipment.setNumber(System.currentTimeMillis());
        slkjEquipment.setCreateTime(DateUtils.getNowDate());
        slkjEquipment.setCreateBy(SecurityUtils.getUsername());
        return slkjEquipmentMapper.insertSlkjEquipment(slkjEquipment);
    }

    /**
     * 修改设备
     * 
     * @param slkjEquipment 设备
     * @return 结果
     */
    @Override
    public int updateSlkjEquipment(SlkjEquipment slkjEquipment)
    {
        Long id = slkjEquipment.getId();
        return slkjEquipmentMapper.updateSlkjEquipment(slkjEquipment);

        // List<SlkjEquipmentShop> slkjEquipmentShops = slkjEquipmentShopMapper.selectSlkjEquipmentShopListByEquipmentId(id);
        //
        // // 计算设备数量总和
        // long sum = slkjEquipmentShops.stream()
        //         .filter(slkjEquipmentShop -> slkjEquipmentShop.getEquipmentId().equals(id))
        //         .mapToLong(slkjEquipmentShop -> slkjEquipmentShop.getNum())
        //         .sum();
        //
        // Long equipmentNum = slkjEquipment.getNum();
        // if (!equipmentNum.equals(sum)) {
        //     log.info("设备数量与库存设备数量不一致，设备id：" + id);
        // }
        //
        // // 获取设备名称
        // String name = slkjEquipment.getName();
        //
        // // 更新 slkjEquipmentShops 中的 equipmentName
        // List<SlkjEquipmentShop> updatedSlkjEquipmentShops = slkjEquipmentShops.stream()
        //         .peek(shop -> shop.setEquipmentName(name))
        //         .collect(Collectors.toList());
        //
        // // 更新 slkjEquipment
        // slkjEquipment.setUpdateTime(DateUtils.getNowDate());
        // slkjEquipment.setUpdateBy(SecurityUtils.getUsername());
        //
        // // 执行更新操作
        // int result = slkjEquipmentMapper.updateSlkjEquipment(slkjEquipment);
        //
        // // 批量更新 slkjEquipmentShops
        // if(updatedSlkjEquipmentShops.size() > 0){
        //     int batchUpdateResult = slkjEquipmentShopMapper.batchUpdate(updatedSlkjEquipmentShops);
        //     // 日志记录
        //     log.info("批量更新 slkjEquipmentShops 结果: " + batchUpdateResult);
        // }
        //
        //
        //
        //
        // return result;
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteSlkjEquipmentByIds(Long[] ids)
    {
        return slkjEquipmentMapper.deleteSlkjEquipmentByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteSlkjEquipmentById(Long id)
    {
        return slkjEquipmentMapper.deleteSlkjEquipmentById(id);
    }
}
